import '@css/adminPage.css'
import { fetchSellersListApi } from '@utils/api/sellersManage'
import { modifySellersApi } from '@utils/api/sellersManage'

document.querySelector('#sellersList').addEventListener('click', e => {
    location = `sellersManage.html`
})
document.querySelector('#addSellers').addEventListener('click', e => {
    location = `addSellersManage.html`
})

window.addEventListener('DOMContentLoaded', async () => {
    let id = window.location.search.split("=")[1];
    const { code, message, data: { sid, sellersName, sellersAccount, sellersPassword } } = await fetchSellersListApi(id)
    if (code) {
        document.querySelector('#sid').value = sid
        document.querySelector('#sellersName').value = sellersName
        document.querySelector('#sellersAccount').value = sellersAccount
        document.querySelector('#sellersPassword').value = sellersPassword
    }
    document.querySelector('#newSellersBtn').addEventListener('click', async () => {

        let sid = document.querySelector("#sid").value
        let sellersName = document.querySelector("#sellersName").value
        let sellersAccount = document.querySelector("#sellersAccount").value
        let sellersPassword = document.querySelector("#sellersPassword").value
        const sellers = { sid, sellersName, sellersAccount, sellersPassword }
        const seller = JSON.stringify(sellers)
        const { code, message } = await modifySellersApi(seller)
        if (code) {
            alert(message)
            location =`sellersManage.html`
        }
    })
})


